import '../models/version_model.dart';
import '../manger/network_utils.dart';
import 'package:shared_preferences/shared_preferences.dart';

typedef VersionCallback = void Function(int reminderType, bool isShow);

class GetVersionManager {
  GetVersionManager._internal();

  static final GetVersionManager shared = GetVersionManager._internal();

  NewVersionModel? newModel;

  Future<void> getNewVersion(VersionCallback callback) async {
    try {
      final response = await NetworkUtils.get(RequestCmd.getVersion);
      final model = VersionModel.fromJson(response);

      if (model.code == 200 && model.data != null) {
        newModel = model.data;
        callback(model.data!.reminderType, true);
      } else {
        callback(0, false);
      }
    } catch (e) {
      callback(0, false);
    }
  }
}

class VersionManager {
  static const _key = 'newVersionShow';

  static Future<void> saveShownVersion(String version) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_key, version);
  }

  static Future<String?> getShownVersion() async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getString(_key);
  }
}